<?php
class Workflowmodel extends CI_Model {

	function fetchhtmlfromservice($param){
		switch ($param) {
			case "total":
				echo "<fieldset id='total' class='step'>";
				echo "<h3 class='wizard-header'><b>TOTAL</b></h3>";
				$this->calculateTotal();
				echo "</fieldset>";
				break;
			case "caltax":
				echo "<fieldset id='caltax' class='step'>";
				echo "<h3 class='wizard-header'><b>Calculate Tax</b></h3>";
				$this->calculateTax();
				echo "</fieldset>";
				break;
			case "giftwrap":
				echo "<fieldset id='giftwrap' class='step'>";
				echo "<h3 class='wizard-header'><b>Gift Wrap</b></h3>";
				$this->wrapGift();
				echo "</fieldset>";
				break;
			case "shipping":
				echo "<fieldset id='shipping' class='step'>";
				echo "<h3 class='wizard-header'><b>Shipping charges</b></h3>";
				$this->shippingCharges();
				echo "</fieldset>";
				break;

			case "prodsuggestion":
				echo "<fieldset id='prodsuggestion' class='step'>";
				echo "<h3 class='wizard-header'><b>Product Suggestion</b></h3>";
				$this->similiarProductSuggestions();
				echo "</fieldset>";
				break;
			case "makepayment":
				echo "<fieldset id='makepayment' class='step'>";
				echo "<h3 class='wizard-header'><b>Make Payment</b></h3>";
				$this->makePayment();
				echo "</fieldset>";
				break;
			case "Confirmation":
				echo "<fieldset id='confirmation' class='step'>";
				echo "<h3 class='wizard-header'><b>Confirmation</b></h3>";
				$this->confirmation();
				echo "</fieldset>";
				break;
			case "promotion":
				echo "<fieldset id='promotion' class='step'>";
				echo "<h3 class='wizard-header'><b>Promotion</b></h3>";
				$this->promotions();
				echo "</fieldset>";
				break;

			case "emailinvoice":
				echo "<fieldset id='emailInvoice' class='step'>";
				echo "<h3 class='wizard-header'><b>Email Invoice</b></h3>";
				$this->mailInvoice();
				echo "</fieldset>";
				break;

			case "coupon":
				echo "<fieldset id='coupon' class='step'>";
				echo "<h3 class='wizard-header'><b>Avail Coupon</b></h3>";
				//$this->applyCoupon($data);
				echo "</fieldset>";
				break;

			case "creditcard":
				echo "<fieldset id='creditcard' class='step'>";
				echo "<h3 class='wizard-header'><b>Process Credit Card</b></h3>";
				echo "<label>Code:</label><input type='text' id='credit_card_valid_text'/><span>&nbsp;</span><button id='creditcard_valid'>Validate</button>";
				echo "<div id='credit-card-div'></div>";
				echo "<script  type='text/javascript'>$('#creditcard_valid').button().click(function(){var c=$('#credit_card_valid_text').val();console.log(length(c));});</script>";
				echo "</fieldset>";
				break;

			case "billprocess":
				echo "<fieldset id='billprocess' class='step'>";
				echo "<h3 class='wizard-header'><b>TOTAL</b></h3>";
				echo "</fieldset>";
				break;

			case "captcha":
				echo "<fieldset id='' class='step'>";
				echo "<h3 class='wizard-header'><b>TOTAL</b></h3>";
				//$this->getCaptcha();
				echo "</fieldset>";
				break;
		}
	}

	public function calculateTotal(){

		if(!is_null($this->session->userdata('cart'))){
			$data=$this->session->userdata('cart');
		}


		$text="";
		$totalprice=0;
		$totalitems=0;
		foreach(json_decode($data) as $key=>$val){
			$text=$text.'<ul style="background-color:#FFC" class="fielddiv ui-widget ui-helper-clearfix" >';
			$text=$text.'<li class="ui-state-default ui-corner-all delfield" ><span class="ui-icon ui-icon-check"></span></li>';
			$text=$text.'<li style="width:70%">'.$val->name.'</li>';
			$text=$text.'<li style="width:10%">'.$val->price.'</li>';
			$text=$text.'<li style="width:10%">'.$val->count.'</li></ul>';
			$totalprice+=$val->price*$val->count;
			$totalitems+=$val->count*1;
		}

		$text=$text.'<ul style="background-color:#CCF; padding:10px;" class="fielddiv ui-widget ui-helper-clearfix" >';
		$text=$text.'<li >&nbsp;&nbsp;&nbsp;&nbsp;</li>';
		$text=$text.'<li style="width:70%"><b>Grand Total</b></li>';
		$text=$text.'<li style="width:10%"><b>$'.$totalprice.'</b></li>';
		$text=$text.'<li style="width:10%"><b>'.$totalitems.'</b></li>';

		$text=$text.'</ul>';
		$this->session->set_userdata('total_price', $totalprice);
		$this->session->set_userdata('total_items',$totalitems);
		echo $text;

	}

	public function getProduct($item){
		switch($item){
			case "A1":
			 return array('name'=>'Book Reader','price'=>189);
			 	
			case "A2":
				return array('name'=>'Book Reader Outer Cover','price'=>18);
			case "A3":
				return array('name'=>'Screen Protector','price'=>10);
			case "A4":
				return array('name'=>'Ear Phone','price'=>64);
			case "B1":
				return array('name'=>'Book :: Programming in Java','price'=>120);
			case "B2":
				return array('name'=>'Book :: Programming in PHP','price'=>89);
			case "B3":
				return array('name'=>'Book :: Programming in ASP','price'=>102);
			case "B4":
				return array('name'=>'Book :: Programming with WebServices','price'=>74);


		}
	}
	public function calculateTax(){

		$total=	$this->session->userdata('total_price');
		$quantity=$this->session->userdata('total_items');
		$text="";

		$amount=$total*1.12;

		$text=$text.'<ul style="background-color:#FFC" class="fielddiv ui-widget ui-helper-clearfix" >';
		$text=$text.'<li style="width:70%">Total Price</li>';
		$text=$text.'<li style="width:20%">'.$total.'</li></ul>';

		$text=$text.'<ul style="background-color:#FFC" class="fielddiv ui-widget ui-helper-clearfix" >';
		$text=$text.'<li style="width:70%">Tax</li>';
		$text=$text.'<li style="width:20%">12%</li></ul>';

		$text=$text.'<ul style="background-color:#CCF; padding:10px;" class="fielddiv ui-widget ui-helper-clearfix" >';
		$text=$text.'<li style="width:70%">Total With Tax</li>';
		$text=$text.'<li style="width:20%">$'.$amount.'</li></ul>';

		$this->session->set_userdata('total_price', $amount);
		echo $text;


	}

	public function applyCoupon(){
		echo "<div id='couponresult'>&nbsp;</button>";
		echo "<input type='text' id='coupontext'/><br>";

		echo "<button id='couponbtn'>Apply Coupon</button>";
	}

	public function shippingCharges(){
		$total=	$this->session->userdata('total_price');
		$quantity=$this->session->userdata('total_items');
		$text="";

		$amount=$total+$quantity*10;

		$text=$text.'<ul style="background-color:#FFC" class="fielddiv ui-widget ui-helper-clearfix" >';
		$text=$text.'<li style="width:70%">Total Price</li>';
		$text=$text.'<li style="width:20%">'.$total.'</li></ul>';

		$text=$text.'<ul style="background-color:#FFC" class="fielddiv ui-widget ui-helper-clearfix" >';
		$text=$text.'<li style="width:70%">Shipping charges</li>';
		$text=$text.'<li style="width:20%">$10 * '.$quantity.' items</li></ul>';

		$text=$text.'<ul style="background-color:#CCF; padding:10px;" class="fielddiv ui-widget ui-helper-clearfix" >';
		$text=$text.'<li style="width:70%">Total with Shipping Charges</li>';
		$text=$text.'<li style="width:20%">$'.$amount.'</li></ul>';

		$this->session->set_userdata('total_price', $amount);
		echo $text;
		echo 'Shipping address :';
		echo '<div style="padding:10px;" id="makepaymentdiv">';
		echo '  <ul>';
		echo '  <li>';
		echo '    <label>First Name:</label>';
		echo '    <input name="fname" type="text" value="" size="19" maxlength="60">';
		echo '  </li>';		echo '  <li>';
		echo '    <label>Last Name:</label>';
		echo '    <input name="lname" type="text" value="" size="19" maxlength="60">';
		echo '  </li>';
		echo '  <li>';
		echo '    <label>Address 1:</label>';
		echo '    <input name="add1" type="text" value="" size="40" maxlength="60">';
		echo '  </li>';
		echo '  <li>';
		echo '    <label>Address 2:</label>';
		echo '    <input name="add2" type="text" value="" size="40" maxlength="60">';
		echo '  </li>';
		echo '  <li>';
		echo '    <label>City:</label>';
		echo '    <input name="city" type="text" value="" size="40" maxlength="60">';
		echo '  </li>';
		echo '  <li>';
		echo '    <label>State:</label>';
		echo '    <input name="state" type="text" value="" size="19" maxlength="60">';
		echo '  </li>';
		echo '  </ul>';
		echo '  </div>';
		
	}

	public function confirmation(){
			if(!is_null($this->session->userdata('cart'))){
			$data=$this->session->userdata('cart');
		}

		$total=	$this->session->userdata('total_price');
		echo "All the below items will be dispatched to you in 3 working days.";
		$text="";
		$totalprice=0;
		$totalitems=0;
		foreach(json_decode($data) as $key=>$val){
			$text=$text.'<ul style="background-color:#FFC" class="fielddiv ui-widget ui-helper-clearfix" >';
			$text=$text.'<li style="width:90%">'.$val->name.'</li></ul';
		}
		
		$text=$text.'<br><br><ul style="background-color:#CCF; padding:10px;" class="fielddiv ui-widget ui-helper-clearfix" >';
		$text=$text.'<li >&nbsp;&nbsp;&nbsp;&nbsp;</li>';
		$text=$text.'<li style="width:70%"><b>Total payment inclusive of Taxes and other charges.</b></li>';
		$text=$text.'<li style="width:10%"><b>$'.$total.'</b></li>';
		$text=$text.'</ul>';
		$text=$text.'<div id="shipping-confirmation"></div>';
		
		echo $text;
		

	}

	public function confirmationViaEmail(){
		echo "<div id='emailresult'>&nbsp;</button>";
		echo "<input type='text' id='emailtext'/>";

		echo "<button id='emailbtn'>Apply Coupon</button>";
	}

	public function mailInvoice(){
		echo "<div style='padding:30px'>";
		echo "<h1>Thanks for Shopping with us.</h1>";
		echo "<br><br>";
		echo "Click on Submit to Email the Invoice.";
		echo "</div>";
	}

	public function wrapGift(){
		echo "<div style='padding:30px'>";
		echo "<input type='checkbox' id='giftwrap' name='giftwrap'/> Add Gift Wrap to all the items.(Free gift wrapping till ThanksGiving).";
		echo "<br><br><br><b>What is Gift Wrap?</b>";
		echo "<p>Gift wrapping refers to the act of enclosing a gift in some sort of material.
		 Wrapping paper is a kind of paper designed for gift wrapping. Gifts may also be wrapped in a box.
		 They may be held closed with ribbon and topped with a decorative bow (an ornamental knot made of ribbon).</p>";
		echo "</div>";
	}

	public function promotions(){
		echo "<div style='padding:30px'>";
		echo "<ul>";
		echo "<li>Get 30 Off on Shirts. Use [XCFVED]</li>";
		echo "<li>Get 20 Off on Jackets. Use [XASDED]</li>";
		echo "<li>Get 10 Off on Books. Use [XCDVED]</li>";
		echo "<li>Get 15 Off on Cosmetics. Use [SDFVED]</li>";
		echo "</ul></div>";
		
	}

	public function getCaptcha(){
		echo "<div id='captcharesult'>&nbsp;</button>";
		echo "What is 2+3 ??<br>";
		echo "<input type='text' id='captchatext'/><br>";

		echo "<button id='captchabtn'>Validate</button>";
	}

	public function similiarProductSuggestions(){
		echo "<div style='padding:30px'>";
		echo "<ul>";
		echo "<li>Buy RayBan Googles.</li>";
		echo "<li>Buy Shoes.</li>";
		echo "<li>Pen Torch</li>";
		echo "</ul></div>";
	}

	public function makePayment(){
		$total=	$this->session->userdata('total_price');
		echo '<div style="padding:10px;" id="makepaymentdiv">';
		echo '  <ul>';
		echo '  <li>';
		echo '    <label>First Name:</label>';
		echo '    <input name="fname" type="text" value="" size="19" maxlength="40">';
		echo '  </li>';		
		echo '  <li>';
		echo '    <label>Last Name:</label>';
		echo '    <input name="lname" type="text" value="" size="19" maxlength="40">';
		echo '  </li>';
		echo '  <li>';
		echo '    <label>Address 1:</label>';
		echo '    <input name="add1" type="text" value="" size="19" maxlength="40">';
		echo '  </li>';		
		echo '  <li>';
		echo '    <label>Address 2:</label>';
		echo '    <input name="add2" type="text" value="" size="19" maxlength="40">';
		echo '  </li>';		
		echo '  <li>';
		echo '    <label>City:</label>';
		echo '    <input name="city" type="text" value="" size="19" maxlength="40">';
		echo '  </li>';		
		echo '  <li>';
		echo '    <label>State:</label>';
		echo '    <input name="state" type="text" value="" size="19" maxlength="40">';
		echo '  </li>';
		echo '  <li>';
		echo '    <label>Total Amount:</label>';
		echo '    <input visible="false" name="amount" type="text" value="'.$total.'" size="19" maxlength="40">';
		echo '  </li>';
		echo '  <li>';
		echo '    <label>Credit Card Number:</label>';
		echo '    <input name="ccno" type="text" value="" size="19" maxlength="40">';
		echo '  </li>';
		echo '  <li>';
		echo '  <label>Expiry Date:</label>';
		echo '      <SELECT NAME="CCExpiresMonth" >';
		echo '        <OPTION VALUE="" SELECTED>--Month--';
		echo '        <OPTION VALUE="01">January (01)';
		echo '        <OPTION VALUE="02">February (02)';
		echo '        <OPTION VALUE="03">March (03)';
		echo '        <OPTION VALUE="04">April (04)';
		echo '        <OPTION VALUE="05">May (05)';
		echo '        <OPTION VALUE="06">June (06)';
		echo '        <OPTION VALUE="07">July (07)';
		echo '        <OPTION VALUE="08">August (08)';
		echo '        <OPTION VALUE="09">September (09)';
		echo '        <OPTION VALUE="10">October (10)';
		echo '        <OPTION VALUE="11">November (11)';
		echo '        <OPTION VALUE="12">December (12)';
		echo '      </SELECT> /';
		echo '      <SELECT NAME="CCExpiresYear">';
		echo '        <OPTION VALUE="" SELECTED>--Year--';
		echo '        <OPTION VALUE="04">2004';
		echo '        <OPTION VALUE="05">2005';
		echo '        <OPTION VALUE="06">2006';
		echo '        <OPTION VALUE="07">2007';
		echo '        <OPTION VALUE="08">2008';
		echo '        <OPTION VALUE="09">2009';
		echo '        <OPTION VALUE="10">2010';
		echo '        <OPTION VALUE="11">2011';
		echo '        <OPTION VALUE="12">2012';
		echo '        <OPTION VALUE="13">2013';
		echo '        <OPTION VALUE="14">2014';
		echo '        <OPTION VALUE="15">2015';
		echo '      </SELECT>';
		echo '  </li>';
		echo '  <li>';
		echo '    <label>Contact Email:</label>';
		echo '    <input name="email" type="text" value="" size="50">';
		echo '  </li>';
		echo '  </ul>';
		echo '</div>';

	}

	function getworkflowarray($wfid){
		
		$this->db->select('*');
		$this->db->from('workflow');
		$this->db->join('WorkflowServiceMapping', 'WorkflowServiceMapping.workflow_id = workflow.WorkflowID');
		$this->db->join('service', 'WorkflowServiceMapping.service_id = service.ServiceID');
		$this->db->where('WorkflowServiceMapping.workflow_id', $wfid);
		$this->db->where('screen_position >', 0);
		$this->db->order_by('screen_position asc, screen_order asc');
		$query=$this->db->get();

		
		$workFlowArray=array();
		if($query->num_rows()  > 0){
			

			//Get first row and populate workflow data
			$row = $query->row();
			$workFlowArray['workflow_service_id'] = $row->workflow_id;
			$workFlowArray['workflow_name'] = $row->name;

			$servicesArray=array();
			foreach ($query->result() as $row)
			{
				$service=array();
				$service['ws_id']=$row->ws_id;
				$service['service_id']=$row->service_id;
				$service['descr']=$row->descr;
				$service['screen_position']=$row->screen_position;
				$service['screen_order']=$row->screen_order;
					
				$servicesArray[] = $service;
			}

			$workFlowArray['service_array']=$servicesArray;

			
		}

		return $workFlowArray;
	}


}
